package cn.afactory.tygz.ui.base.utils.sns;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsCommentInfo;


public class CommentTextUtil {

    /**
     * 获得完整内容
     *
     * @param ctx
     * @param info
     * @param needContent
     * @return
     */
    public static String getCententText(Context ctx, SnsCommentInfo info, boolean needContent) {
        String content = "";
//        if (needContent) {
//            content += getRelayText(ctx, info);
//            content += ":" + (info.textContent != null ? info.textContent : "");
//        } else {
//            content += getRelayText(ctx, info);
//        }
        return content;
    }

    /**
     * 获得个人回复内容
     *
     * @param ctx
     * @param info
     * @return
     */
    public static String getRelayText(Context ctx, SnsCommentInfo info) {
        String content = "";
//        if (info != null) {
//            content += info.createUserInfo != null && info.createUserInfo.nick != null ? info.createUserInfo.nick : "";
//            content += info.replyToUserInfo != null && info.replyToUserInfo.nick != null ? " " + ctx.getString(R.string.health_subject_reply) + " " + info.replyToUserInfo.nick : "";
//        }
        return content;
    }

    /**
     * 设置字体颜色
     *
     * @param ctx
     * @param tv
     * @param info
     * @param needContent
     */
    public static void setTextView(Context ctx, TextView tv, SnsCommentInfo info, boolean needContent) {

        SpannableStringBuilder mSpannableStringBuilder;
        String chr = "";
        if (needContent) {
            chr = getCententText(ctx, info, true);
            mSpannableStringBuilder = new SpannableStringBuilder(chr);
            setSpannableText(ctx, info, chr, mSpannableStringBuilder);
            setSpannableText(ctx, getRelayText(ctx, info).length(), chr.length()
                    , mSpannableStringBuilder);
        } else {
            chr = getRelayText(ctx, info);
            mSpannableStringBuilder = new SpannableStringBuilder(chr);
            setSpannableText(ctx, info, chr, mSpannableStringBuilder);
        }

        tv.setText(mSpannableStringBuilder);

    }

    /**
     * 设置 回复姓名部分的字体颜色
     *  @param ctx
     * @param info
     * @param chr
     * @param ssb
     */
    private static void setSpannableText(Context ctx, SnsCommentInfo info, String chr, SpannableStringBuilder ssb) {
        int nlenth = 0;
//        if (info.createUserInfo != null && info.createUserInfo.nick != null) {
//            nlenth = info.createUserInfo != null && info.createUserInfo.nick != null ? info.createUserInfo.nick.length() : 0;
//
//            ssb.setSpan(
//                    new ForegroundColorSpan(ctx.getResources().getColor(R.color.black)), 0,
//                    nlenth, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//        }
//
//        if(info.replyToUserInfo != null && info.replyToUserInfo.nick != null){
//            ssb.setSpan(
//                    new ForegroundColorSpan(ctx.getResources().getColor(R.color.black)),
//                    nlenth, nlenth + 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//
//            nlenth = nlenth + 4;
//            ssb.setSpan(
//                    new ForegroundColorSpan(ctx.getResources().getColor(R.color.black)), nlenth,
//                    chr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//        }
    }

    /**
     * @param ctx
     * @param i
     * @param length
     * @param ssb
     * @Description 设置回复内容颜色
     * @author xiezhidong@pajk.cn
     */

    private static void setSpannableText(Context ctx, int i, int length, SpannableStringBuilder ssb) {
        ssb.setSpan(
                new ForegroundColorSpan(ctx.getResources().getColor(R.color.black)),
                i, length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    }
}
